package com.system.cacheCenterBackend.controller.redisDataController;

import com.system.cacheCenterBackend.controller.hotKeyController.HotKeyController;
import com.system.cacheCenterBackend.service.HashCacheService;
import com.system.cacheClient.common.CacheR;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Hash缓存控制器
 * 提供Hash类型的RESTful API接口
 */
@RestController
@RequestMapping("/cache/hash")
@RequiredArgsConstructor
public class HashCacheController {

    private final HashCacheService hashCacheService;

    /**
     * 设置Hash缓存值
     *
     * @param key   Hash键
     * @param value Hash值
     * @return 统一返回结果
     */
    @PostMapping("/{key}")
    public CacheR<Boolean> set(@PathVariable String key, @RequestBody Map<String, Object> value) {
        try {
            try {
                hashCacheService.hashPut(key, value);
                HotKeyController.addKey("hash", key);
                return CacheR.success(true, "设置Hash缓存成功");
            } catch (Exception e) {
                return CacheR.fail("设置Hash缓存失败");
            }
        } catch (Exception e) {
            return CacheR.fail("设置Hash缓存异常: " + e.getMessage());
        }
    }

    /**
     * 获取Hash缓存值
     *
     * @param key Hash键
     * @return 统一返回结果
     */
    @GetMapping("/{key}")
    public CacheR<Map<String, String>> get(@PathVariable String key) {
        try {
            Map<String, String> value = hashCacheService.hashGetAll(key);
            if (value != null) {
                return CacheR.success(value, "获取Hash缓存成功");
            } else {
                return CacheR.fail(404, "Hash缓存不存在");
            }
        } catch (Exception e) {
            return CacheR.fail("获取Hash缓存异常: " + e.getMessage());
        }
    }

    /**
     * 删除Hash缓存值
     *
     * @param key Hash键
     * @return 统一返回结果
     */
    @DeleteMapping("/{key}/{value}")
    public CacheR<Boolean> delete(@PathVariable String key, @PathVariable String value) {
        try {
            if (hashCacheService.hashDelete(key, value) != 0) {
                // 从热点数据控制器中移除键
                HotKeyController.removeKey("hash", key);
                return CacheR.success(true, "删除Hash缓存成功");
            } else {
                return CacheR.fail("删除Hash缓存失败");
            }
        } catch (Exception e) {
            return CacheR.fail("删除Hash缓存异常: " + e.getMessage());
        }
    }

    /**
     * 设置Hash缓存过期时间
     *
     * @param key     Hash键
     * @param timeout 过期时间（秒）
     * @return 统一返回结果
     */
//    @PostMapping("/{key}/expire")
//    public CacheR<Boolean> expire(@PathVariable String key, @RequestParam long timeout) {
//        try {
//            boolean result = hashCacheService.hash(key, timeout);
//            if (result) {
//                return CacheR.success(true, "设置过期时间成功");
//            } else {
//                return CacheR.fail("设置过期时间失败");
//            }
//        } catch (Exception e) {
//            return CacheR.fail("设置过期时间异常: " + e.getMessage());
//        }
//    }
}